// pages/topay/topay.js
var app=getApp()
var url=app.globalData.url
var url1 = app.globalData.url1
var orderId=''
var sumPrice=0
var totalPrice=0
var couponId
Page({

  /**
   * 页面的初始数据
   */
  data: {
  names:"付款",
  flag:true,
    coupons:[1,2],
  },
  back(){
    wx.navigateBack(1)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    orderId=options.orderId
        this.getMEssages(options.orderId)
  },
  chooseCoupon(e){
   var couponId=e.currentTarget.dataset.id
    this.setData({
      couIndex:e.currentTarget.dataset.index
    })
   this.getTotalprice(couponId)
  },
  getTotalprice(couponId){
    const that=this
    wx.request({
      url: url + '/cw_order/details_pay',
      data: {
        couponId:couponId,
        totalPrice: sumPrice  //之前总价
      },
      header: {
        "content-type": "application/json"
      },
      success(res) {
        console.log(res.data)
        if (res.data.code === 1) {
          totalPrice = res.data.data.totalPrice
          that.setData({
            totalPrice: totalPrice
          })
        }
      }
    })
  },
  getMEssages(orderId){
    const that=this
    wx.request({
      url: url+'/cw_order/details_pay',
      data:{
        orderId: orderId
      },
      header:{
        "content-type":"application/json"
      },
      success(res){
        console.log(res.data)
        if(res.data.code===1){
          orderId  = res.data.data.orderMaster.orderId
          sumPrice = res.data.data.orderMaster.orderAmount
          totalPrice = res.data.data.orderMaster.orderAmount
          that.setData({
            orderMsg:res.data.data,
            totalPrice: totalPrice
          })
        }
      }
    })
  },

  //支付
  pay: function () {
    //先修改订单补差价
    var openid = wx.getStorageSync('openid')

          console.log("")
          wx.request({
            url: url1+'/weixin/auth',
            data: {
              openid: openid,
              sumprice: totalPrice,
              ordersId: orderId
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log("返回：" + res)
              var that = this
              wx.requestPayment({//調支付
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': 'MD5',
                'paySign': res.data.paySign,
                'success': function (res) {
                  console.log('success');
                  wx.showToast({
                    title: '支付成功',
                    icon: 'success',
                    duration: 3000

                  })
                  app.globalData.currentTab = 2,
                    wx.navigateTo({ url: '../order/order' })
                },
                'fail': function (res) {
                  wx.showToast({
                    title: '支付失败',
                    icon: 'loading',
                    duration: 3000
                  });
                  that.pay()
                },
              });
            }
          })

  },

})